/*
 * Created on Sep 2, 2005
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.numbersix.ant.locetask;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;

import org.apache.commons.lang.CharSetUtils;

public abstract class AbstractFileTypeLOCECounter {
	
	private int setCount = 0;	
	private final int SIZE = 500;
	
	public abstract String getFileType();

	void processFile(File f) throws FileNotFoundException, IOException {
		int totalCount = 0;
		if (f.canRead()) {
			char[] chunk = new char[SIZE];
			StringBuffer sb = new StringBuffer(SIZE);
			String value = null;
			FileReader reader = new FileReader(f);
			int count; 
			while (reader.read(chunk) != -1) {
				count = 0;
				sb.delete(0, sb.length());
				sb.append(chunk);
				value = sb.toString();
				count = CharSetUtils.count(value, getLineMarkers());
				totalCount += count;
				Arrays.fill(chunk, '.');				
			}
			
			//System.out.println("File '" + f.getName() + "' has " + totalCount + " lines");
			
			setCount += totalCount;
		}
	}
	
	public abstract String[] getLineMarkers();
	
	public int getSetCount() {
		return setCount;
	}

}
